﻿if (typeof Modules == "undefined") Modules = {};
//------------------------------------------------------------------------------
//Create by: tantajnguyen@gmail.com
//Create date: 2014/03/25
//Desctition: load layout
//------------------------------------------------------------------------------
Modules.Layout = {
    Type: '',
    BaseUrl: '',

    init: function () {
        var _this = this;
        _this.Type = Modules.Core.QueryString.Type() || VTNT.Type;

        var callback = function (layout, isAddBody) {
            var $html;
            if (layout) {
                $html = $(layout);
            }
            else {
                $html = $(Modules.Core.Default.LayoutContent);
            }
            _this.load($html, isAddBody);
        };
        var layout;
        if (VTNT && VTNT.Layout) {
            layout = $.HtmlDecode(VTNT.Layout);
            delete VTNT.Layout;

            callback(layout, true);
        }
        else {
            var template = Modules.Core.CoreFunction.getTemplateView();
            layout = template + '/view/layout.htm ' + Modules.Core.Default.LayoutContent;

            $('body').load(layout, function (data) {
                callback();
            });
        }
    },
    load: function ($html, isAddBody) {
        var _this = this,
            aInput = [], aEl = [];
        $html.find('[' + Modules.Core.Default.Input + ']').each(function (idx, el) {
            var $el = $(el);
            aEl.push($el);
            aInput.push(Modules.Core.CoreFunction.getDataInput($el));
        });
        if (aInput.length) {
            var input = '<' + aInput.join(' /><') + '/>';
            input = input.replace(/'/gi, '"');
            Modules.ServiceData.GetContextData(input, function (data) {
                if (data) {
                    for (var i = 0; i < aEl.length; i++) {
                        Modules.Core.View.init(aEl[i], data[i + 1]);
                    }
                }
                if (isAddBody) {
                    $('body').html('').append($html);
                }
                //load page view content
                if (typeof initResource != 'undefined' && $.isFunction(initResource)) {
                    initResource('#content-page');
                }
            });
        }
    },
    actionLink: function () {
        if (this.BaseUrl == '') {
            this.BaseUrl = '?Type=' + this.Type;
        }
        return this.BaseUrl;
    },
    homeLink: function () {
        return VTNT.Web_HostName;
    },
    getResourceView: function (type) {
        var ret = 'product';
        type = type || this.Type;
        switch (type) {
            case '26': //news cats
            case '27': //news list            
                ret = 'news';
                break;

            case '31': //layout
                ret = 'info';
                break;

            case '2':
            case '5':
                ret = 'product';
                break;
        }
        return ret;
    }
};

$(function () {
    Modules.Layout.init();
});